import type { VarTypes } from '@/types/common'
import { getVarType } from '@/utils/common'

// 域名正则
const domainRegex = /^([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.[a-zA-Z]{2,}$/
// IP 正则
const ipRegex = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/

/**
 * 验证类型
 */
export function validateType(v: any, types: VarTypes[]) {
  return types.includes(getVarType(v))
}

/**
 * 验证值
 */
export function validateValue(v: any, values: any[]) {
  return values.includes(v)
}

/**
 * 检查主机地址
 */
export function validHost(host: string) {
  return host.toLowerCase() === 'localhost' || domainRegex.test(host) || ipRegex.test(host)
}
